package com.woniuxy.educationplatform.mapper;

import com.woniuxy.educationplatform.bean.CourseBean;
import com.woniuxy.educationplatform.bean.SubjectBean;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.Date;
import java.util.List;

@Mapper
public interface CourseMapper {
    @Results({
            @Result(id = true, property = "id", column = "id", javaType = Long.class),
            @Result(property = "courseName", column = "course_name", javaType = String.class),
            @Result(property = "createTime", column = "create_time", javaType = Date.class),
            @Result(property = "subjectBean", column = "fk_subject", javaType = SubjectBean.class, one =
            @One(select = "com.woniuxy.educationplatform.mapper.SubjectMapper.findById", fetchType = FetchType.LAZY))
    })
    @Select("select * from course where id= #{id}")
    CourseBean findById(Long id);

    @Results({
            @Result(id = true, property = "id", column = "id", javaType = Long.class),
            @Result(property = "courseName", column = "course_name", javaType = String.class),
            @Result(property = "createTime", column = "create_time", javaType = Date.class),
            @Result(property = "subjectBean", column = "fk_subject", javaType = SubjectBean.class, one =
            @One(select = "com.woniuxy.educationplatform.mapper.SubjectMapper.findById", fetchType = FetchType.LAZY))
    })
    @Select("select * from course")
    List<CourseBean> findAll();

    @Results({
            @Result(id = true, property = "id", column = "id", javaType = Long.class),
            @Result(property = "courseName", column = "course_name", javaType = String.class),
            @Result(property = "createTime", column = "create_time", javaType = Date.class),
            @Result(property = "subjectBean", column = "fk_subject", javaType = SubjectBean.class, one =
            @One(select = "com.woniuxy.educationplatform.mapper.SubjectMapper.findById", fetchType = FetchType.LAZY))
    })

    List<CourseBean> findOneByCondition(@Param("cname") String courseName,@Param("sid") Long id);


    @Delete("delete from course where id =#{id}")
    void delete(Long id);

    /**
     * 新建
     *
     * 初步分析是fk_subject可以从saveSubjectBean()返回的SubjectBean中获取，并存到表中，创建时会一并
     *
     * 另一种情况是：下拉框（只能从已有的项目中选取）每个选项给一个值，将这个值返回到这个存储语句中。
     * @param courseBean
     * @return
     */
    @Insert("insert into course (course_name,fk_subject,create_time) values (#{c.courseName},#{c.subjectBean.id},now())")
    void saveCourseBean(@Param("c") CourseBean courseBean);

    /**
     * 修改
     *
     * 修改对应的sid和名字即可
     *
     * @param courseBean
     */

    @Update("update course set course_name = #{c.courseName} where id = #{c.id}")
    void updateCourseBean(@Param("c") CourseBean courseBean);


    @Select("select course_name from course")
    String[] getCourseName();
}
